package com.sky.business.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.business.sys.domain.CameraConfig;
import com.sky.business.sys.mapper.CameraConfigMapper;
import com.sky.business.sys.service.CameraConfigService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CameraConfigServiceImpl extends ServiceImpl<CameraConfigMapper, CameraConfig> implements CameraConfigService {

    @Override
    public CameraConfig findByDeviceId(String deviceId) {
        LambdaQueryWrapper<CameraConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CameraConfig::getCameraDeviceId, deviceId);
        return getBaseMapper().selectOne(wrapper);
    }

    @Override
    public List<CameraConfig> findByDeviceIdIn(List<String> deviceIdList) {
        LambdaQueryWrapper<CameraConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(CameraConfig::getCameraDeviceId, deviceIdList);
        return getBaseMapper().selectList(wrapper);
    }

    @Override
    public CameraConfig findByIpAddrAndPort(String ip,int port) {
        LambdaQueryWrapper<CameraConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CameraConfig::getCameraIp, ip);
        queryWrapper.eq(CameraConfig::getCameraPort,String.valueOf(port));
        return getBaseMapper().selectOne(queryWrapper);
    }

    @Override
    public List<CameraConfig> listIdleCamera(Date lastHeartbeat) {
        return getBaseMapper().listIdleCamera(lastHeartbeat);
    }

    @Override
    public List<CameraConfig> listIdleByDeviceIn(List<String> deviceIdList,Date lastHeartbeat) {
        return getBaseMapper().listIdleByDeviceIn(deviceIdList,lastHeartbeat);
    }

    @Override
    public void updateHeartbeat(Long cameraConfigId , Date heartbeatTime) {
        LambdaUpdateWrapper<CameraConfig> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(CameraConfig::getLastHeartBeatTime, heartbeatTime).eq(CameraConfig::getId, cameraConfigId);
        getBaseMapper().update(updateWrapper);
    }

    @Override
    public void updateOnvifDeviceInfo(Long cameraConfigId, String manufacturer, String model, String firmwareVersion, String serialNumber) {
        LambdaUpdateWrapper<CameraConfig> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(CameraConfig::getManufacturer, manufacturer).set(CameraConfig::getModel,model)
                .set(CameraConfig::getFirmwareVersion,firmwareVersion).set(CameraConfig::getSerialNumber,serialNumber)
                .eq(CameraConfig::getId, cameraConfigId);
        getBaseMapper().update(updateWrapper);
    }

}
